import 'dart:io';

import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(new LocationPlugin());
}

/*
 * 定位组件 <br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class LocationPlugin extends StatefulWidget {
  State<StatefulWidget> createState() {
    return new LocationPluginState();
  }
}

/*
 * 页面功能 <br/>
 */
class LocationPluginState extends State<StatefulWidget> {
  void initState() {
    super.initState();
    AmapCore.init("4babab115580829d4001b5e3d515376b");
  }

  Widget build(BuildContext context) {
    AppBar appBar = AppBar(
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back),
        onPressed: () {},
      ),
      title: new Text('FlutterDesign'),
      centerTitle: true,
      actions: <Widget>[
        new Container(width: 0, height: 0),
        new Container(width: 0, height: 0),
      ],
      backgroundColor: Colors.blue,
    );

    return MaterialApp(
      home: new Scaffold(
        appBar: appBar,
        body: new Builder(builder: (BuildContext context) {
          return Padding(
            child: Card(
              child: Row(
                children: <Widget>[
                  RaisedButton(
                    child: Text('测试'),
                    onPressed: () async {
                      if (await requestPermission()) {
                        final location = await AmapLocation.fetchLocation();
                        location.address.then((value) => print(value));
                        location.latLng.then((value) => print(value));
                      }
                    },
                  ),
                ],
              ),
            ),
            padding: EdgeInsets.all(10),
          );
        }),
        backgroundColor: Colors.white,
        bottomNavigationBar: null,
      ),
    );
  }
}

Future<bool> requestPermission() async {
  final permissions =
      await PermissionHandler().requestPermissions([PermissionGroup.location]);

  if (permissions[PermissionGroup.location] == PermissionStatus.granted) {
    return true;
  } else {
    print('需要定位权限!');
    return false;
  }
}
